﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PADWH35
{
    public partial class ResinRefillDialog : Form
    {
        private Timer switchFormTimer = new Timer();

        public ResinRefillDialog()
        {
            InitializeComponent();
        }

        public ResinRefillDialog(Int16 IntRun)
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;

            if (IntRun == 1 || IntRun == 2)
            {
                tabControl1.SelectedTab = IntRun == 1 ? t1 : t2;
                switchFormTimer.Enabled = false;
            }
            else
            {
                switchFormTimer.Interval = 50000;
                switchFormTimer.Enabled = true;
            }
            switchFormTimer.Tick += SwitchFormTimer_Tick;

            panel_Error.Visible = false;
            gv.AutoGenerateColumns = false;
        }

        private void SwitchFormTimer_Tick(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabControl1.SelectedTab == t1 ? t2 : t1;
        }

        public void BindData()
        {
            try
            {
                panel_Error.Visible = false;
                string sql = "select * from resin_refill_view";
                gv.DataSource = DBHelper.ExecuteDt(sql);
                for (int i = 0; i < gv.Rows.Count; i++)
                {

                    if (gv.Rows[i].Cells[ActualRefillleadtimeCol.Index].Value.ToString() == "999")
                    {
                        gv.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(0, 234, 106); //Color.Green;
                        label13.BackColor = Color.FromArgb(0, 234, 106);
                        continue;
                    }

                    if (int.Parse(gv.Rows[i].Cells[ActualAlertstopdateCol.Index].Value.ToString()) < 0)
                    {
                        gv.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                    else if (int.Parse(gv.Rows[i].Cells[ActualAlertstopdateCol.Index].Value.ToString()) == 0)//这里从没有变过蓝色
                    {
                        gv.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(117, 163, 255);//Color.Blue;
                        label15.BackColor = Color.FromArgb(117, 163, 255);
                    }
                    else if (int.Parse(gv.Rows[i].Cells[ActualAlertstopdateCol.Index].Value.ToString()) <= 15)
                    {
                        gv.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    }
                    else//罗经理让改除红,黄,绿外,全部变蓝色,第一个画面没有白色.
                    {
                        gv.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(117, 163, 255);//Color.Blue;
                        label15.BackColor = Color.FromArgb(117, 163, 255);
                    }
                }

                Application.DoEvents();
                sql = "select * from machine_layout_view";

                DataTable d = DBHelper.ExecuteDt(sql);

                setMachineData(t2, d);
            }
            catch (Exception ex)
            {
                panel_Error.Visible = true;
                lblError.Text = ex.Message;
            }
        }

        private void setMachineData(Control c, DataTable d)
        {
            try
            {
                Label l;
                for (int i = 0; i < c.Controls.Count; i++)
                {
                    if (c.Controls[i].Controls.Count > 0)
                    {
                        Application.DoEvents();
                        setMachineData(c.Controls[i], d);
                        continue;
                    }

                    if (c.Controls[i] is Label)
                    {
                        l = (Label)c.Controls[i];
                        DataRow[] dr = d.Select("lizenz='" + l.Name + "'");
                        if (dr.Length > 0)
                        {
                            if (int.Parse(dr[0]["ActualAlertstopdate"].ToString()) <= 0 && int.Parse(dr[0]["WERT"].ToString()) == 1)
                            {
                                l.BackColor = Color.Red;
                            }
                            else if (int.Parse(dr[0]["ActualAlertstopdate"].ToString()) <= 15 && int.Parse(dr[0]["WERT"].ToString()) == 1)
                            {
                                l.BackColor = Color.Yellow;
                            }
                            else if (int.Parse(dr[0]["WERT"].ToString()) == 1)
                            {
                                l.BackColor = Color.FromArgb(117, 163, 255);//Color.Blue;
                                label6.BackColor = Color.FromArgb(117, 163, 255);
                            }
                            else if (int.Parse(dr[0]["WERT"].ToString()) == 0)
                            {
                                l.BackColor = Color.FromArgb(0, 234, 106); // Color.Green; 
                                label2.BackColor = Color.FromArgb(0, 234, 106);
                            }
                            else
                            {
                                l.BackColor = Color.White;
                            }
                        }
                        else
                        {
                            if (l.Parent is GroupBox)
                            {
                                l.BackColor = Color.White;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                panel_Error.Visible = true;
                lblError.Text = ex.Message;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = 60000;
            BindData();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            lbl_Time.Text = "当前时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
